package com.kaisu.blog.infrastructure.blog;

import com.kaisu.blog.infrastructure.file.FilePO;
import com.kaisu.blog.infrastructure.user.UserPO;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

/**
 * @author kaisu
 */
@Getter
@Setter
@Entity
@ToString
@DynamicInsert
@DynamicUpdate
@Table(name = "`blog`")
public class BlogPO {
    /**
     * id
     */
    @Id
    private String id;
    /**
     * 标题
     */
    private String title;
    /**
     * 封面
     */
    private String fileId;
    /**
     * 内容
     */
    private String content;
    /**
     * 状态
     */
    private String status;
    /**
     * 创建用户
     */
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "create_user_id")
    private UserPO createUser;
    /**
     * 最后更新时间
     */
    private LocalDateTime lastModifyTime;
    /**
     * 发布时间
     */
    private LocalDateTime publishedTime;
    /**
     * 点赞
     */
    @OneToMany(mappedBy = "blog", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
    @ToString.Exclude
    private Set<LikePO> likes;
    /**
     * 评论
     */
    @ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinTable(
            name = "blog_comment",
            joinColumns = {
                    @JoinColumn(name = "blog_id"),
            },
            inverseJoinColumns = {
                    @JoinColumn(name = "comment_id")
            }
    )
    private List<CommentPO> comments;
    /**
     * 创建时间
     */
    @CreationTimestamp
    private LocalDateTime createTime;
    /**
     * 更新时间
     */
    @UpdateTimestamp
    private LocalDateTime updateTime;
}
